package com.demo;

import com.demo.impl.GreetAboutTimeImpl;
import com.demo.impl.GreetToUserImpl;

import java.util.Calendar;
import java.util.Date;

public class RefactorThreeApplication {
    public static void main(String[] args) {
        HelloWorld helloWorld = initialize();
        sayHelloInTheMorning(helloWorld);
        sayHelloInTheAfternoon(helloWorld);
        sayHelloAtNight(helloWorld);
    }

    private static HelloWorld initialize() {
        HelloWorld helloWorld = new HelloWorld();
        helloWorld.setGreetToUser(new GreetToUserImpl());
        helloWorld.setGreetAboutTime(new GreetAboutTimeImpl());
        return helloWorld;
    }

    private static void sayHelloInTheMorning(HelloWorld helloWorld) {
        String user = "John";
        Date now = getDate(2020, 1, 1, 9);
        String result = helloWorld.sayHello(now, user);
        System.out.println(result);
    }

    private static void sayHelloInTheAfternoon(HelloWorld helloWorld) {
        String user = "Mary";
        Date now = getDate(2020, 1, 1, 13);
        String result = helloWorld.sayHello(now, user);
        System.out.println(result);
    }

    private static void sayHelloAtNight(HelloWorld helloWorld) {
        String user = "Jack";
        Date now = getDate(2020, 1, 1, 20);
        String result = helloWorld.sayHello(now, user);
        System.out.println(result);
    }

    private static Date getDate(int year, int month, int date, int hour) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month, date, hour, 0);
        return calendar.getTime();
    }
}
